package cn.dugcrs.org.service.impl;

import cn.dugcrs.basic.exception.BusinessException;
import cn.dugcrs.basic.utils.BaiduAuditUtils;
import cn.dugcrs.org.domain.Employee;
import cn.dugcrs.org.domain.Shop;
import cn.dugcrs.org.domain.ShopAuditLog;
import cn.dugcrs.org.mapper.EmployeeMapper;
import cn.dugcrs.org.mapper.ShopAuditLogMapper;
import cn.dugcrs.org.mapper.ShopMapper;
import cn.dugcrs.org.service.IShopService;
import cn.dugcrs.basic.service.impl.BaseServiceImpl;
import cn.dugcrs.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gubcog
 * @since 2022-05-04
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    /* 
    * @Title: 店铺入驻
    * @Description: TODO
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/5/4 16:42  
    * @Parameters: * @param 
    * @Return void
    */
    @Override
    public void settlement(Shop shop) {
        // 空校验
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名不能为空！");
        }
        if (StringUtils.isEmpty(shop.getTel())) {
            throw new BusinessException("座机号不能为空！");
        }
        if (StringUtils.isEmpty(shop.getAddress())) {
            throw new BusinessException("地址不能为空！");
        }
        if (StringUtils.isEmpty(shop.getLogo())) {
            throw new BusinessException("请上传logo图片！");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getUsername())) {
            throw new BusinessException("请输入账号！");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getPhone())) {
            throw new BusinessException("请输入手机号！");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getEmail())) {
            throw new BusinessException("请输入邮箱！");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getPassword())) {
            throw new BusinessException("请输入密码！");
        }
        if (StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())) {
            throw new BusinessException("请再次密码！");
        }

        // 判断两次密码是否一致
        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())) {
            throw new BusinessException("两次密码不一致，请检查后重试！");
        }

        // 店铺名非法判断，百度智能
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名非法，请重新取名！");
        }

        // 店铺名字重复
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null) {
            throw new BusinessException("店铺名已存在，请重新取名！");
        }

        // 判断账户是否已经被注册
        Employee admin = shop.getAdmin();
        Employee employeeTmp = employeeMapper.loadByUserName(admin.getUsername());
        // 如果已经注册了用户了，并且绑定了店铺则直接登录
        if(employeeTmp != null && employeeTmp.getShop_id() != null){
            throw new BusinessException("该账号已经绑定过店铺，请直接登陆！！");
        }

        // 如果用户还没注册，那么店铺也不可能存在，则应该先添加用户
        if(employeeTmp == null){
            employeeMapper.save(admin); // 返回自增ID
            employeeTmp = admin;    // 新增的员工赋值给employeeTmp
        }

        // 到这里账户已存在，但是还没有绑定店铺
        // 直接将employeeTmp 绑定给shop
        shop.setAdminId(employeeTmp.getId());
        shopMapper.save(shop);  // 返回自增Id
        Shop shop2 = new Shop();
        shop2.setId(shop.getId());
        employeeTmp.setShop(shop2);
        employeeMapper.update(employeeTmp);
        
    }

    /*
    审核驳回
    店铺状态：0待审核，1已审核，-1驳回，2待激活
     */
    @Override
    public void reject(ShopAuditLog shopLog) {
        // 把店铺的状态设置为 -1
        Shop shop = shopMapper.loadById(Long.valueOf(shopLog.getShopId()));
        shop.setState(-1);
        shopMapper.update(shop);

        // 记录日志
        shopLog.setAuditId(325); // 没做登录id暂且写死
        shopAuditLogMapper.save(shopLog);

        // 发送邮件，带上激活链接通知店铺管理员去激活店铺
        Employee employee = employeeMapper.loadByShopId(Long.valueOf(shopLog.getShopId()));
        SimpleMailMessage simple = new SimpleMailMessage();
        simple.setFrom("3133046533@qq.com");
        simple.setTo("3133046533@qq.com");
        simple.setSubject(shop.getName() + "：你的审核已驳回-驳回邮件");
        simple.setText("亲，您的修改连接是：" +
                "<a href='http://localhost:8080/shop/activation?shopId="+shopLog.getShopId()+"'>点击修改</a>");
        javaMailSender.send(simple);

    }

    /*
    审核通过
    店铺状态：0待审核，1已审核，-1驳回，2待激活
     */
    @Override
    public void pass(ShopAuditLog shopLog) {
        // 把店铺的状态设置为 2
        Shop shop = shopMapper.loadById(Long.valueOf(shopLog.getShopId()));
        shop.setState(2);
        shopMapper.update(shop);

        // 记录日志
        shopLog.setAuditId(325); // 没做登录id暂且写死
        shopAuditLogMapper.save(shopLog);

        // 发送邮件，带上激活链接通知店铺管理员去激活店铺
        Employee employee = employeeMapper.loadByShopId(Long.valueOf(shopLog.getShopId()));
        SimpleMailMessage simple = new SimpleMailMessage();
        simple.setFrom("3133046533@qq.com");
        simple.setTo(employee.getEmail());
        simple.setSubject(shop.getName() + "：你的审核通过-激活邮件");
        simple.setText("亲，您的激活连接是：" +
                "<a href='http://localhost:8080/shop/activation?shopId="+shopLog.getShopId()+"'>点击激活</a>");
        javaMailSender.send(simple);
    }

    /*
    excel导入批量添加
     */
    @Override
    public void saveAll(List<Shop> shops) {
        shopMapper.saveAll(shops);
    }

    /*
     * 不同状态的店铺数量报表原始数据
     */
    @Override
    public Map<String,Object> loadEachars() {
        List<ShopVo> voList = shopMapper.loadEachars();
        // 报表横坐标
        List<Integer> x = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        // 报表纵坐标
        List<Integer> y = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return map;
    }
}
